package cn.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Code78 {
    public static void main(String[] args) {
        Code78Demo code78Demo = new Code78Demo();
        List<List<Integer>> subsets = code78Demo.subsets(new int[]{1,2});
        System.out.println(subsets.toString());
    }
}


class Code78Demo {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        ArrayList<Integer> container = new ArrayList<>();
        backtrack(nums, result, container, 0);

        return result;
    }

    private void fun(int[] nums, List<List<Integer>> result, ArrayList<Integer> container, int flag){
        for (int i=0; i< nums.length; i++){
            if(flag > i){
                continue;
            }
            container.add(nums[i]);
            fun(nums, result, container, i+1);
            container.remove(container.size()-1);
        }
        result.add(new ArrayList<>(container));
    }

    public void backtrack(int[] nums, List<List<Integer>> res, List<Integer> sub,  int i) {
        for(int j = i; j<nums.length; j++){
            sub.add(nums[j]);
            backtrack(nums, res, sub, j+1);
            sub.remove(sub.size()-1);
        }
        res.add(new ArrayList<>(sub));
    }




}